package com.moon.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.moon.R;
import com.moon.handle.ImageLoader;
import com.moon.model.FoundModel;

/**
 * @author paul
 * @date 2014年5月5日
 */
public class FoundAdapter extends BaseAdapter {
    private LayoutInflater   mInflater;
    public ImageLoader       imageLoader;
    private List<FoundModel> list;

    public FoundAdapter(Context context, List<FoundModel> list) {
        this.list = list;
        this.mInflater = LayoutInflater.from(context);
        imageLoader= new ImageLoader(context);

    }

    /**
     * 当ListView数据发生变化时,调用此方法来更新ListView
     * @param list
     */
    public void updateListView(List<FoundModel> list){
        this.list = list;
        notifyDataSetChanged();
    }
    
    @Override
    public int getCount() {
        return this.list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        // final SortModel mContent = list.get(position);
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.found_item_adapter, null);
            viewHolder.tvHead = (ImageView) convertView.findViewById(R.id.head_img);
            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.title);
            //viewHolder.tvLetter = (TextView) convertView.findViewById(R.id.catalog);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        imageLoader.DisplayImage(this.list.get(position).getImgSrc(), viewHolder.tvHead);
        viewHolder.tvTitle.setText(this.list.get(position).getTitle());
        return convertView;
    }

}

class ViewHolder {
    ImageView tvHead;
    TextView  tvLetter;
    TextView  tvTitle;
}
